package org.liaohailong.victorlibrary.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import org.liaohailong.library.widget.MTNavigationLayout
import org.liaohailong.victorlibrary.R
import org.liaohailong.victorlibrary.base.BaseActivity

/**
 * Author: liaohailong
 * Date: 2020/7/25
 * Time: 9:40
 * Description: 仿照美团app-外卖模块，底部弹性跳动导航栏
 **/
class NavigationBarActivity : BaseActivity(), View.OnClickListener {
    companion object {
        fun show(context: Context) {
            val intent = Intent(context, NavigationBarActivity::class.java)
            context.startActivity(intent)
        }
    }

    private lateinit var navLayout: MTNavigationLayout

    private val viewList = ArrayList<View>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_bar)

        navLayout = findViewById(R.id.nav_layout)

        val homeContainer = findViewById<View>(R.id.fl_home)
        val classContainer = findViewById<View>(R.id.fl_class)
        val findContainer = findViewById<View>(R.id.fl_find)
        val mineContainer = findViewById<View>(R.id.fl_mine)

        homeContainer.setOnClickListener(this)
        classContainer.setOnClickListener(this)
        findContainer.setOnClickListener(this)
        mineContainer.setOnClickListener(this)

        val homeImg = findViewById<View>(R.id.iv_home)
        val classImg = findViewById<View>(R.id.iv_class)
        val findImg = findViewById<View>(R.id.iv_find)
        val mineImg = findViewById<View>(R.id.iv_mine)

        viewList.add(homeImg)
        viewList.add(classImg)
        viewList.add(findImg)
        viewList.add(mineImg)
    }

    override fun onClick(v: View?) {
        v?.apply {
            // 执行动画的下标
            val index = when (id) {
                R.id.fl_home -> 0
                R.id.fl_class -> 1
                R.id.fl_find -> 2
                R.id.fl_mine -> 3
                else -> 0
            }

            val selectSuccess = navLayout.select(index, object : MTNavigationLayout.AnimCallback {

                override fun onAnimProgress(index: Int, progress: Float, offsetValue: Int) {
                    // icon图片跟着动一波
                    viewList[index].translationY = -offsetValue.toFloat()
                }
            })
        }
    }
}